package com.cskaoyan.service.vx;

import com.cskaoyan.bean.bo.vxBo.AddressBo;
import com.cskaoyan.bean.po.wxPo.MarketAddress;
import com.cskaoyan.bean.po.wxPo.MarketAddressExample;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.bean.vo.vxVo.AddressListVo;
import com.cskaoyan.mapper.wxMapper.MarketAddressMapper;
import com.cskaoyan.mapper.wxMapper.MarketRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: DYQ
 * @time: Created on 2022/6/7 11:33
 */

@Service
public class AddressServiceImpl implements AddressService{

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public TVO<MarketAddress> queryAddressList(Integer id) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andUserIdEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);
        for (MarketAddress marketAddress : marketAddresses) {
            marketAddress.setPostalCode(null);
        }
        Integer total = marketAddressMapper.countByExampleReturnInteger(example);
        Integer pages = 1;

        return TVO.data(total, pages, total, pages, marketAddresses);
    }

    @Override
    public AddressListVo queryAddressDetail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        AddressListVo addressListVo = AddressListVo.data(marketAddress);
        return addressListVo;
    }

    @Override
    public MarketAddress addAddress(AddressBo addressBo, Integer userId) {

        System.out.println("==========================");
        System.out.println(addressBo);
        System.out.println("==========================");


        Date date = new Date();
        MarketAddress marketAddress = new MarketAddress();
        marketAddress.setName(addressBo.getName());
        marketAddress.setUserId(userId);
        marketAddress.setProvince(addressBo.getProvince());
        marketAddress.setCity(addressBo.getCity());
        marketAddress.setCounty(addressBo.getCounty());
        marketAddress.setAddressDetail(addressBo.getAddressDetail());
        marketAddress.setTel(addressBo.getTel());
        marketAddress.setAreaCode(addressBo.getAreaCode());
        marketAddress.setPostalCode(marketRegionMapper.selectPostalCodeByCountry(addressBo.getCounty(),
                addressBo.getCity()));
        marketAddress.setIsDefault(addressBo.getIsDefault());
        marketAddress.setDeleted(false);
        if (addressBo.getId() == 0) {
            marketAddress.setId(null);
            marketAddress.setAddTime(date);
            marketAddress.setUpdateTime(date);
            marketAddressMapper.insert(marketAddress);
        } if (addressBo.getId() != 0) {
            marketAddress.setId(addressBo.getId());
            marketAddress.setUpdateTime(date);
            marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
        }
        return marketAddress;
    }

    @Override
    public void deleteAddressById(Integer id) {
        MarketAddress marketAddress = new MarketAddress();
        marketAddress.setId(id);
        marketAddress.setDeleted(true);
        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
    }
}
